package com.example.tnot.Util;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
// CircularLayout.java
public class CircularLayout extends ViewGroup {
    private float radius = 200f; // 弧形半径

    public CircularLayout(Context context) {
        super(context);
    }

    public CircularLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int childCount = getChildCount();
        int centerX = getWidth() / 2;
        int centerY = getHeight() / 2;

        // 计算起始角度和角度间隔
        float startAngle = 180f; // 从左侧开始
        float angleInterval = 180f / (childCount - 1); // 在半圆内分布

        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                // 计算子视图位置
                float angle = (float) Math.toRadians(startAngle + i * angleInterval);
                int childWidth = child.getMeasuredWidth();
                int childHeight = child.getMeasuredHeight();

                int x = (int) (centerX + radius * Math.cos(angle) - childWidth / 2);
                int y = (int) (centerY + radius * Math.sin(angle) - childHeight / 2);

                child.layout(x, y, x + childWidth, y + childHeight);
            }
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        // 设置最大尺寸
        int maxWidth = Math.min(width, 550); // 最大宽度
        int maxHeight = Math.min(height, 550); // 最大高度

        measureChildren(widthMeasureSpec, heightMeasureSpec);

        setMeasuredDimension(maxWidth, maxHeight);

    }
}
